package com.gitee.cheungming.order.feign;

import com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancer;
import com.gitee.cheungming.common.Result;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Configuration
@Component
@FeignClient(name = "mall-goods")
@LoadBalancerClient(name = "mall-goods", configuration = NacosLoadBalancer.class)
public interface GoodsClient {

    /**
     * 扣减库存
     */
    @RequestMapping("/goods/reduceStock")
    Result<?> reduceStock(@RequestParam("id") Long id, @RequestParam("count") Integer count);

    /***
     * 服务降级和分布式事务是冲突的，因为分布式事务回滚是需要抛出异常的，而服务降级会吞掉异常的。
     * 因此我们需要区分一个业务是否是核心业务，如果是核心业务，则需要使用分布式事务，否则使用服务降级。
     * @return
     */
    @CircuitBreaker(name = "mall-goods", fallbackMethod = "fallback")
    @RequestMapping("/goods/testLoadBalance")
    Result<?> testLoadBalance();

    default Result<?> fallback(Throwable e) {
        return Result.error("服务调用失败");
    }
}
